//
//  YZMStockUnitView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/25.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMStockUnitView.h"

@interface YZMStockUnitView()

@property (nonatomic, strong) UILabel *titleLB;
//@property (nonatomic, strong) UIButton *explainBT;
@property (nonatomic, strong) UIView *topLine;
@property (nonatomic, strong) UIView *bottomLine;

@end

@implementation YZMStockUnitView

- (instancetype)init{
    
    self = [super initWithFrame:CGRectMake(0, 0, 1, 1)];
    if (self) {
        
        self.backgroundColor = [YZMAppStyle colorForMain];
        [self createObjects];
    }
    
    return self;
}

- (void)setTitle:(NSString *)title{
    
    self.titleLB.text = title;
}

- (void)setTitleAttString:(NSAttributedString *)titleAttString {
    self.titleLB.attributedText = titleAttString;
}

- (void)setTitleFont:(UIFont *)titleFont {
    _titleFont = titleFont;
    self.titleLB.font = titleFont;
}

- (void)setExplainAction:(void (^)())explainAction{
    
    _explainAction = explainAction;
    if (_explainAction) {
        
        _explainBT.hidden = NO;
        
    }else{
        
        _explainBT.hidden = YES;
    }
}

- (void)setBottomLineType:(YZMStockUnitLineType)bottomLineType{
    
    if (bottomLineType == YZMStockUnitLineTypeForNone) {
        
        self.bottomLine.hidden = YES;
        return;
    }
    
    self.bottomLine.hidden = NO;
    
    [self.bottomLine mas_updateConstraints:^(MASConstraintMaker *make) {
        switch (bottomLineType) {
            case YZMStockUnitLineTypeForAll: {
                make.left.equalTo(self);
                break;
            }
            case YZMStockUnitLineTypeForMarginLeft: {
                make.left.equalTo(self).offset(15);
                break;
            }
            default:
                break;
        }
    }];
}

- (void)setTopLineType:(YZMStockUnitLineType)topLineType{
    
    if (topLineType == YZMStockUnitLineTypeForNone) {
        
        self.topLine.hidden = YES;
        return;
    }
    
    self.topLine.hidden = NO;
    
    [self.topLine mas_updateConstraints:^(MASConstraintMaker *make) {
        switch (topLineType) {
            case YZMStockUnitLineTypeForAll: {
                make.left.equalTo(self);
                break;
            }
            case YZMStockUnitLineTypeForMarginLeft: {
                make.left.equalTo(self).offset(15);
                break;
            }
            default:
                break;
        }
    }];
}

- (void)setRightView:(UIView *)rightView{
    
    [self addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-15);
    }];
}

- (void)setRightSecondView:(UIView *)rightSecondView{
    [self addSubview:rightSecondView];
    [rightSecondView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@100);
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).with.offset(-70);
        make.height.equalTo(self);
    }];
}

- (void)setRightLargeView:(UIView *)rightLargeView{
    
    [self addSubview:rightLargeView];
    [rightLargeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLB);
        make.right.equalTo(self).offset(-15);
    }];
    
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(rightLargeView).offset(30);
    }];
}

#pragma mark -- event

- (void)clickExplain:(UIButton *)sender{
    
    if (_explainAction) {
        _explainAction();
    }
}

#pragma mark -- ui

- (void)createObjects{
    
    self.titleLB = [YZMAppStyle labelWithMediumWhite];
    [self addSubview:self.titleLB];
    [self.titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(15);
        make.left.equalTo(self).offset(15);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.titleLB).offset(15);
    }];
    
    self.explainBT = [YZMAppStyle explainButton];
    [self addSubview:self.explainBT];
    [self.explainBT addTarget:self action:@selector(clickExplain:) forControlEvents:UIControlEventTouchUpInside];
    self.explainBT.hidden = YES;
    [self.explainBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLB.mas_right);
        make.centerY.equalTo(self.titleLB);
    }];
    
    self.topLine = [YZMAppStyle splitLine];
    self.topLine.hidden = YES;
    [self addSubview:self.topLine];
    [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
    }];
    
    self.bottomLine = [YZMAppStyle splitLine];
    self.bottomLine.hidden = YES;
    [self addSubview:self.bottomLine];
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self);
        make.right.equalTo(self).offset(-15);
    }];
}

@end
